package app.agent;

import app.env.Data;
import app.env.Position;
import app.env.ShowGui;
import framework.agent.CustomAgent;
import jade.core.behaviours.OneShotBehaviour;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @Name: SearchAgent
 * @Description: 包含属性（position，data）负责搜集地图上的信息。并将搜
 * 集到的信息数据封装到ACLMessage类中，并将收集到的信息数据使用Jade提供的黄
 * 页服务和FIPA发送给管理者代理。
 * @Author: colfish
 * @Date: 2021/3/27
 * 好好学习，天天向上！
 */
public class SearchAgent extends CustomAgent {

    private static final long serialVersionUID = -8879493751646994721L;

    private Logger logger = LogManager.getLogger(this.getClass());
    public int range = Schedule.searchRange;

    // 自身所处的位置
    public Position position;

    public Integer signal = 0;

    // 发现的目标位置
    public Position targetPosition;
    //
    /**
     * 是否发现了目标代理
     * false未发现目标
     * true 发现了目标
     */
    public boolean findTarget = false;

    public int[][] searchArea;

    // 行为种类
    private static final String init = "init";
    private static final String searchSignal = "searchSignal";
    private static final String waitMessage = "waitMessage";
    private static final String end = "end";


    @Override
    protected void setup() {

        super.setup();
        addBehaviour(new Init(this));
    }

    public class Init extends OneShotBehaviour {
        private static final long serialVersionUID = -4354331778064731669L;
        private final SearchAgent agent;

        public Init(SearchAgent agent){
            this.agent = agent;
        }

        @Override
        public void action() {
            Data.searchAgents.add(agent);
            agent.init();
            logger.info("SearchAgent: <" + agent.getLocalName() + "> Init Finished...");
        }
    }

    /**
     * 初始化Agent信息
     */
    public void init(){
        // 通过Admin传递的参数获取自身位置
        position = (Position) getArguments()[0];
        searchArea = new int[2*range][2*range];
        // 初始化搜索区域信号数组
        for(int i = 0; i < 2*range; i++){
            for(int j = 0; j < 2*range; j++) {
                searchArea[i][j] = 0;
            }
        }
        searchArea[range][range] = Data.SEARCH_SIGNAL;
    }

    /**
     * 计算搜索代理周围的信号之和，并判断是否找到了目标代理5 15
     */
    public void calculateSignal(){
        boolean find = false;
        signal = 0;
        targetPosition = null;
        for(int i = position.x-range; i < position.x+range; i++){
            for(int j = position.y-range; j < position.y+range; j++){
                // 搜索到自己的位置，位置大于搜索范围，位置超出边界，跳过
                if( i==position.x&&j==position.y
                    ||position.getDistance(i,j)>range
                    ||i < 0 || i >= Data.MAP_HEIGHT
                    ||j < 0 || j >= Data.MAP_WIDTH)
                    continue;
                // 搜索到的信号是目标代理类型
                if(ShowGui.board[i][j] == Data.TARGET_SIGNAL){
                    targetPosition = new Position(i,j);
                    find = true;
                }
                // 搜索到的信号是目标代理散发的信号值范围
                if(ShowGui.board[i][j] > 0 && ShowGui.board[i][j] < ShowGui.MAX_SIGN_NUM){
                    signal += ShowGui.board[i][j];
                }
                searchArea[i-position.x+range][j-position.y+range] = ShowGui.board[i][j];
            }
        }
        findTarget = find;
    }
}
